package com.example.administrator.hellocomp;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class recorderActivity extends Activity {
    private MediaRecorder recorder;
//    private String
    private File sdpath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorder);

        if (ActivityCompat.checkSelfPermission( recorderActivity.this,
                Manifest.permission.PROCESS_OUTGOING_CALLS)
                != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions( recorderActivity.this,
                    new String[]{ Manifest.permission.PROCESS_OUTGOING_CALLS }, 123);
            return;
        }
        if (ActivityCompat.checkSelfPermission( recorderActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions( recorderActivity.this,
                    new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 123);
            return;
        }
        if (ActivityCompat.checkSelfPermission( recorderActivity.this,
                Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions( recorderActivity.this,
                    new String[] { Manifest.permission.RECORD_AUDIO }, 123);
            return;
        }
        Button btn;
        btn = (Button)findViewById(R.id.btn_recorder);
//        btn.setImageResource(R.drawable.play);

        sdpath= Environment.getExternalStorageDirectory(); //获得手机SD卡路径
//            File path=new File(sdpath+"//Music//recorder//");



        btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try{
                            recorder = new MediaRecorder();
                            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                            recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
                            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                            recorder.setOutputFile(sdpath.toString()+"/Music/recorder/"+System.currentTimeMillis()+".amr");
                            recorder.prepare();
                            recorder.start();

                        }catch (IOException er)
                        {
                            er.printStackTrace();
                        }
                        Toast.makeText(getApplicationContext(),"开始录音了哦",Toast.LENGTH_SHORT).show();
                    }
                }
        );

        Button btn2 = (Button) findViewById(R.id.btn_stop);
        btn2.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(recorder!=null)
                        {
                            recorder.stop();
                            recorder.release();
                            recorder=null;
                            Toast.makeText(getApplicationContext(),"结束录音了哦",Toast.LENGTH_SHORT).show();

                        }

                    }
                }
        );




    }
}
